import { createStore } from "redux";
function fn(state ={carList:[]}, action) {
    switch (action.type) {
        case "jia":
            let s1=JSON.parse(JSON.stringify(state))
            let index=s1.carList.findIndex(v=>v.id===action.obj.id);
            if(index===-1){
                action.obj.num=1;
                s1.carList.push(action.obj)
            }else{
                s1.carList[index].num +=1
            }
            state=s1;
            return state;
        // case "jian":
        //     return state -= action.n;
        default:
            return state;
    }
}
const store = createStore(fn)
export default store;
// import { createStore } from "redux";
// const store = createStore(fn)
// function fn(state=2,action){
//     switch(action.type){
//         case "jia":
//             return state +=action.n;
//             case "jian":
//             return state -= action.n;
//             default:
//                 return state;
//     }
// }
// //调用state
// console.log(store.getState());
// //手动订阅 -当state变化时立即执行
// store.subscribe(() => {
//     console.log(store.getState());
// });
// store.dispatch({type:"jia",n:8});